# 导入应用实例
from app import app

def generate_api_docs(app):
    """生成API文档"""
    docs = ["# API文档\n\n"]
    
    for rule in sorted(app.url_map.iter_rules(), key=lambda x: x.rule):
        if not rule.endpoint.startswith('static'):
            docs.append(f"## {rule.rule}\n")
            docs.append(f"- 方法: {', '.join(rule.methods - {'HEAD', 'OPTIONS'})}\n")
            
            # 获取函数文档字符串
            view_func = app.view_functions[rule.endpoint]
            if view_func.__doc__:
                docs.append(f"- 描述: {view_func.__doc__.strip()}\n")
                
            docs.append("\n")
    
    with open("api_docs.md", "w", encoding="utf-8") as f:
        f.write("".join(docs))
    
    print("API文档已生成到 api_docs.md")

# 在应用上下文中运行
if __name__ == "__main__":
    with app.app_context():
        generate_api_docs(app) 